# shader support
# we use glew and load the ARB functions instead of OpenGL 2.0

import sys
from OpenGL.GL import *
from glew import *

class Shader:

	def __init__(self,filename):
	
		self.types = ["glsl"]
		self.type = ""
		extension = filename[-4:]
		for t in self.types:
			if t == extension:
				self.type = extension
				break
		print("Shaderprograms are of type " + self.type)
		if self.type == "glsl":
			self.prepare = self.prepareGLSL
			self.use = self.useGLSL

		self.prepare(filename)

			
	def prepareGLSL(self,filename):
	
		self.vShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)
		self.fShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB)	
		f = open(filename+".vert")
		vs = f.read()
		f.close()
		g = open(filename+".frag")		fs = g.read()
		g.close()			glShaderSourceARB(self.vShader, 1, vs);		glShaderSourceARB(self.fShader, 1, fs);		#	glCompileShaderARB(self.vShader);	#	glCompileShaderARB(self.fShader);		#	self.program = glCreateProgramObjectARB();			#	glAttachObjectARB(self.program, self.vShader);	#	glAttachObjectARB(self.program, self.fShader);		#	glLinkProgramARB(self.program);
		
	def useGLSL(self):
		glUseProgramObjectARB(self.program)
		
		
class MyMaterial:			def __init__(self,filename):				self.pass_no = 0
				
				# load shaders from file
				f = open(filename+".vert")
				vs = f.read()
				f.close()
				g = open(filename+".frag")				fs = g.read()
				g.close()
								# Create a shader				self.m_program = glCreateProgramObjectARB()				# Compile the vertex shader				self.shader(GL_VERTEX_SHADER_ARB, (vs))				# Compile the fragment shader				self.shader(GL_FRAGMENT_SHADER_ARB, (fs))				# Link the shaders together				self.link()					def shader(self, type, shaders):				# Create a shader object				shader_object = glCreateShaderObjectARB(type)						# Add the source code				glShaderSourceARB(shader_object, len(shaders), shaders)								# Compile the shader				glCompileShaderARB(shader_object)								# Print the compiler log				#self.PrintInfoLog("vertex shader", shader_object)								# Check if compiled, and attach if it did				compiled = glGetObjectParameterivARB(shader_object, GL_OBJECT_COMPILE_STATUS_ARB)				if compiled:					glAttachObjectARB(self.m_program, shader_object)									# Delete the object (glAttachObjectARB makes a copy)				glDeleteObjectARB(shader_object)								# print the gl error log				#self.PrintGLError()							def link(self):				# clear error indicator						glGetError()								glLinkProgramARB(self.m_program)						#self.PrintInfoLog("link", self.m_program)							linked = glGetObjectParameterivARB(self.m_program, GL_OBJECT_LINK_STATUS_ARB)				if not linked:					print "Shader failed to link"					return						glValidateProgramARB(self.m_program)				valid = glGetObjectParameterivARB(self.m_program, GL_OBJECT_VALIDATE_STATUS_ARB)				if not valid:					print "Shader failed to validate"					return							def activate(self):				self.pass_no+=1				if (self.pass_no == 1):					glDisable(GL_COLOR_MATERIAL)					glUseProgramObjectARB(self.m_program)					return True				
			def deactivate(self):				glEnable(GL_COLOR_MATERIAL)				glUseProgramObjectARB(0)				self.pass_no = 0				#	return False		